/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import controladores.ParametrosJpaController;
import controladores.exceptions.NonexistentEntityException;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import modelo.Parametros;

/**
 *
 * @author Ususario
 */
public class ParametrosGenerales extends javax.swing.JInternalFrame {

    ParametrosJpaController pC;
    Parametros p;
    DecimalFormat df = new DecimalFormat();

    public ParametrosGenerales() {
        initComponents();
        df.setMaximumFractionDigits(0);
        pC = new ParametrosJpaController();
        p = pC.findParametros(1);
        salariominimoField.setText("" + df.format(p.getSalariominimo()));
        auxiliotransporteField.setText("" + df.format(p.getAuxiliotransporte()));
        subsidioalimentacionField.setText("" + df.format(p.getSubsidioalimentacion()));
        porcentajesaludField.setText("" + df.format(p.getPorcentajesalud()));
        porcentajepensionField.setText("" + df.format(p.getPorcentajepension()));
        limitesubalimentacion.setText("" + df.format(p.getLimitesubalimentacion()));

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        limitesubalimentacion = new javax.swing.JTextField();
        porcentajepensionField = new javax.swing.JTextField();
        salariominimoField = new javax.swing.JTextField();
        salariominimoLabel = new javax.swing.JLabel();
        auxiliotransporteLabel = new javax.swing.JLabel();
        auxiliotransporteField = new javax.swing.JTextField();
        subsidioalimentacionLabel = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        porcentajesaludLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        subsidioalimentacionField = new javax.swing.JTextField();
        porcentajesaludField = new javax.swing.JTextField();
        porcentajepensionLabel = new javax.swing.JLabel();

        setTitle("Parametros generales");
        setPreferredSize(new java.awt.Dimension(885, 440));

        salariominimoLabel.setText("Salario minimo:");

        auxiliotransporteLabel.setText("Auxilio de transporte:");

        subsidioalimentacionLabel.setText("Subsidio de alimentacion:");

        saveButton.setText("Guardar");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        porcentajesaludLabel.setText("% Salud:");

        jLabel1.setText("Limite subsidio alimentación:");

        porcentajepensionLabel.setText("% Pension:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(salariominimoLabel)
                    .addComponent(auxiliotransporteLabel)
                    .addComponent(subsidioalimentacionLabel)
                    .addComponent(porcentajesaludLabel)
                    .addComponent(porcentajepensionLabel)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(salariominimoField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 634, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(auxiliotransporteField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(subsidioalimentacionField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(porcentajesaludField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(porcentajepensionField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(limitesubalimentacion, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {auxiliotransporteField, limitesubalimentacion, porcentajepensionField, porcentajesaludField, salariominimoField, subsidioalimentacionField});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salariominimoLabel)
                    .addComponent(salariominimoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(auxiliotransporteLabel)
                    .addComponent(auxiliotransporteField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(subsidioalimentacionLabel)
                    .addComponent(subsidioalimentacionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(porcentajesaludLabel)
                    .addComponent(porcentajesaludField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(porcentajepensionLabel)
                    .addComponent(porcentajepensionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(limitesubalimentacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(saveButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(39, 39, 39))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(175, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        p = pC.findParametros(1);
        if (salariominimoField.getText().trim().equals("") || auxiliotransporteField.getText().trim().equals("")
                || subsidioalimentacionField.getText().trim().equals("") || porcentajesaludField.getText().trim().equals("")
                || porcentajepensionField.getText().trim().equals("") || limitesubalimentacion.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Por favor llene todos los campos");
        } else {
            try {
                p.setSalariominimo(Float.parseFloat(salariominimoField.getText().trim().replaceAll("\\.", "")));
                p.setAuxiliotransporte(Float.parseFloat(auxiliotransporteField.getText().trim().replaceAll("\\.", "")));
                p.setSubsidioalimentacion(Float.parseFloat(subsidioalimentacionField.getText().trim().replaceAll("\\.", "")));
                p.setPorcentajesalud(Float.parseFloat(porcentajesaludField.getText().trim().replaceAll("\\.", "")));
                p.setPorcentajepension(Float.parseFloat(porcentajepensionField.getText().trim().replaceAll("\\.", "")));
                p.setLimitesubalimentacion(Float.parseFloat(limitesubalimentacion.getText().trim().replaceAll("\\.", "")));
                pC.edit(p);
                JOptionPane.showMessageDialog(null, "Parametros Actualizados con exito");
            } catch (NonexistentEntityException ex) {
                Logger.getLogger(ParametrosGenerales.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(ParametrosGenerales.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }//GEN-LAST:event_saveButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField auxiliotransporteField;
    private javax.swing.JLabel auxiliotransporteLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField limitesubalimentacion;
    private javax.swing.JTextField porcentajepensionField;
    private javax.swing.JLabel porcentajepensionLabel;
    private javax.swing.JTextField porcentajesaludField;
    private javax.swing.JLabel porcentajesaludLabel;
    private javax.swing.JTextField salariominimoField;
    private javax.swing.JLabel salariominimoLabel;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField subsidioalimentacionField;
    private javax.swing.JLabel subsidioalimentacionLabel;
    // End of variables declaration//GEN-END:variables
}
